import { ref } from 'vue'
import type { ConsultOrderItem } from '@/types/consult'
import { deleteOrderById } from '@/services/consult'
import { showSuccessToast, showFailToast } from 'vant'

export function useDelConsult(
  item: ConsultOrderItem,
  cb: (id: string) => void
) {
  const delLoading = ref<boolean>(false)
  // defineEmits只能在setup语法中使用，组合式函数中不能单独使用defineEmits
  // const emits = defineEmits<{
  //   (e: 'onDelOrder', id: string): void
  // }>()

  const handleDel = async () => {
    delLoading.value = true
    try {
      await deleteOrderById(item.id)
      // emits('onDelOrder', item.id) // 想要删除ConsultList组件中的本地数据
      cb && cb(item.id)
      showSuccessToast('删除成功')
    } catch (error) {
      showFailToast('删除失败')
    } finally {
      // 成功之后，让取消的loading隐藏，弹提示
      delLoading.value = false
    }
  }
  return {
    delLoading,
    handleDel
  }
}
